package dao;

import entity.User;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class UserDao {

    //根据用户名用户类型查询用户
    public User getUserByUsernameAndUserType(String username, int userType) {
        String sql = "SELECT * FROM user WHERE username = ? AND user_type = ?";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            preparedStatement.setInt(2, userType);
            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("user_id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setUserType(resultSet.getByte("user_type"));
                user.setPhone(resultSet.getString("phone"));
                user.setCreatedTime(resultSet.getTimestamp("created_time"));
                user.setGender(resultSet.getByte("gender"));
                user.setUpdateTime(resultSet.getTimestamp("update_time"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    //增加用户
    public boolean addUser(User user) {
        String sql = "INSERT INTO user (username, password, user_type, phone, created_time, gender, update_time) VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setInt(3, user.getUserType());
            preparedStatement.setString(4, user.getPhone());
            preparedStatement.setTimestamp(5, new Timestamp(user.getCreatedTime().getTime()));
            preparedStatement.setInt(6, user.getGender());
            preparedStatement.setTimestamp(7, new Timestamp(user.getUpdateTime().getTime()));
            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    //更新用户
    public boolean updateUser(User user) {
        String sql = "UPDATE user SET password = ?, user_type = ?, phone = ?, created_time = ?, gender = ?, update_time = ? WHERE username = ?";
        try (Connection connection = JDBCUtils.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setInt(2, user.getUserType());
            preparedStatement.setString(3, user.getPhone());
            preparedStatement.setTimestamp(4, new Timestamp(user.getCreatedTime().getTime()));
            preparedStatement.setInt(5, user.getGender());
            preparedStatement.setTimestamp(6, new Timestamp(user.getUpdateTime().getTime()));
            preparedStatement.setString(7, user.getUsername());
            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
